package com.pwx.student.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.pwx.student.common.ResponseResult;
import com.pwx.student.dao.RcCourseMapper;
import com.pwx.student.dao.RcStudentCourseMapper;
import com.pwx.student.entity.RcCourse;
import com.pwx.student.entity.RcStudent;
import com.pwx.student.dao.RcStudentMapper;
import com.pwx.student.entity.RcStudentCourse;
import com.pwx.student.service.IRcStudentService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pwx.student.service.bo.ChoseCourseBo;
import com.pwx.student.vo.input.CourseInputVo;
import com.pwx.student.vo.input.StudentInfoInputVo;
import com.pwx.student.vo.output.CourseOutputVo;
import com.pwx.student.vo.output.StudentInfoVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author saber
 * @since 2022-10-24
 */
@Service
public class RcStudentServiceImpl extends ServiceImpl<RcStudentMapper, RcStudent> implements IRcStudentService {

    @Autowired
    private RcStudentMapper studentMapper;
    @Autowired
    private RcStudentCourseMapper rcScMapper;
    @Autowired
    private RcCourseMapper rcCMapper;

    @Override
    public ResponseResult searchCourse(CourseInputVo inputVo) {
        PageHelper.startPage(inputVo.getIndex(),8);
        List<CourseOutputVo> voList = studentMapper.searchCourse(inputVo);
        PageInfo<CourseOutputVo> info = new PageInfo<>(voList);
        return ResponseResult.createSuccess(info);
    }

    @Override
    public ResponseResult choseCourse(Integer courseId, Integer studentId) {
        //1. 课程是否存在
        RcCourse course = rcCMapper.selectOne(new QueryWrapper<RcCourse>()
                .eq("course_id",courseId));
        if (course==null){
            return ResponseResult.createFail("选修课程不存在");
        }
        //2. 学员id正确
        Integer studentGrade = studentMapper.getStudentGrade(studentId);
        if (studentGrade==null){
            return ResponseResult.createFail("学生不存在");
        }
        //3. 学员的年级 和 课程的年级是有一致
        if (!studentGrade.equals(course.getCourseGrade())){
            return ResponseResult.createFail("课程年级和学生年级不一致");
        }
        //4. 学员是否已有选择这个课程
        RcStudentCourse rcStudentCourse = rcScMapper.selectOne(
                new QueryWrapper<RcStudentCourse>()
                        .eq("sc_course_id",courseId)
                        .eq("sc_student_id",studentId));
        if (rcStudentCourse!=null){
            return ResponseResult.createFail("该课程以选修,无法重复");
        }
        //5. 课程人数已超最大人数
        if (course.getCourseMaxSize().equals(course.getCourseSelectedCount())){
            return ResponseResult.createFail("该课程以满,无法选修");
        }
        //6. 上课时间是否重复
        String choseCours = course.getCourseTime();
        List<String> courseList = studentMapper.getStudentCourseList(studentId);
        for (String s:courseList){
            if (ChoseCourseBo.checkCourse(s,choseCours)) {
                return ResponseResult.createFail("该课程时间和已选课程冲突,无法选修");
            }
        }
        //7. 新增人数
        RcStudentCourse rcSc = new RcStudentCourse(studentId,courseId);
        int insert = rcScMapper.insert(rcSc);
        if (insert!=0){
            course.setCourseSelectedCount(course.getCourseSelectedCount()+1);
            int i = rcCMapper.updateById(course);
            if (i!=0){
                return ResponseResult.createSuccess("选课成功");
            }
        }
        return ResponseResult.createFail("选课失败");

    }


    @Override
    public ResponseResult showInfo(Integer studentId) {
       StudentInfoVo studentInfoVo =  studentMapper.showInfo(studentId);
       if (studentInfoVo!=null){
           return ResponseResult.createSuccess(studentInfoVo);
       }
        return ResponseResult.createFail("没有该学生信息");
    }

    @Override
    public ResponseResult changeInfo(StudentInfoInputVo inputVo, Integer studentId) {
        RcStudent student = studentMapper.selectOne(new QueryWrapper<RcStudent>()
                .eq("student_id", studentId));
        if (student==null){
            return ResponseResult.createFail("没有该学生信息");
        }
        if (student.getStudentBirthday()!=null&&student.getStudentBirthday().getTime()>System.currentTimeMillis()) {
            return ResponseResult.createFail("请输入正确的出生日期");
        }
        student.setStudentBirthday(inputVo.getStudentBirthday());
        student.setStudentEmail(inputVo.getStudentEmail());
        student.setStudentPassword(inputVo.getStudentPassword());

        System.out.println("====================");
        System.out.println(student);
        int i = studentMapper.updateById(student);
        if (i>0){
            return ResponseResult.createSuccess("修改成功");
        }
        return ResponseResult.createFail("修改失败");
    }
}
